package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.Arrays;
import java.util.List;

import m.jclecai.com.bean.JclqItme;
import m.jclecai.com.inter.IAdapterChange;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.Utils;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/3/31.
 */

public class BettingJCLQAdapter extends BaseAdapter {
    private Context context;
    private List<JclqItme> list;
    private NotifyTextViewSetChanged changed;
    private int pager = 0;

    public BettingJCLQAdapter(Context context, List<JclqItme> list, NotifyTextViewSetChanged changed,int pager){
        this.pager = pager;
        this.context=context;
        this.list=list;
        this.changed=changed;
    }
    public void clearData(){
        Utils.JCLQ_ITEM_LIST.clear();
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        String[] rqsfstrings = list.get(position).getStrings();
        final List<String[]> rqsflists = FastOk.getList(rqsfstrings[15], String[].class);
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_zq_bet_txo,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.home_team_name.setText(rqsfstrings[4]);
        childViewHolder.away_team_name.setText(rqsfstrings[3]);
//        final DecimalFormat df = new DecimalFormat("#.00");
        childViewHolder.jclq_fen_name.setTextColor(Color.RED);
        childViewHolder.ll1.setVisibility(View.VISIBLE);
        childViewHolder.ll.setVisibility(View.VISIBLE);
        childViewHolder.ll2.setVisibility(View.VISIBLE);
        if(pager == 0){
            childViewHolder.ll2.setVisibility(View.GONE);
            childViewHolder.jclq_fen_name.setText("VS");
            childViewHolder.odd_home.setText("客胜"+rqsflists.get(0)[0]);
            childViewHolder.odd_draw.setText("主胜"+rqsflists.get(0)[1]);
        }else if(pager ==1){
            childViewHolder.ll2.setVisibility(View.GONE);
            childViewHolder.jclq_fen_name.setText(rqsflists.get(1)[0]);
            childViewHolder.odd_home.setText("客胜"+rqsflists.get(1)[1]);
            childViewHolder.odd_draw.setText("主胜"+rqsflists.get(1)[2]);
        }else if(pager == 2){
            childViewHolder.jclq_fen_name.setText(rqsflists.get(2)[0]);
            childViewHolder.odd_home.setText("大"+rqsflists.get(2)[1]);
            childViewHolder.odd_draw.setText("小"+rqsflists.get(2)[2]);
            childViewHolder.ll2.setVisibility(View.GONE);
        }else if(pager == 3){
            childViewHolder.ll.setVisibility(View.GONE);
            childViewHolder.ll2.setVisibility(View.VISIBLE);
            childViewHolder.odd_home.setText(rqsfstrings[4]+"\n"+rqsflists.get(1)[1]);
            childViewHolder.odd_draw.setText(rqsfstrings[3]+"("+rqsflists.get(1)[0]+")\n"+rqsflists.get(1)[2]);
            childViewHolder.odd_home1.setText(">"+rqsflists.get(2)[0]+"分\n"+rqsflists.get(2)[1]);
            childViewHolder.odd_draw1.setText("<"+rqsflists.get(2)[0]+"分\n"+rqsflists.get(2)[2]);
        }

        //右边
        childViewHolder.odd_draw.setBackgroundColor(list.get(position).isSchecked() ? Color.parseColor("#FF0026") : Color.WHITE);
        childViewHolder.odd_draw.setTextColor(list.get(position).isSchecked() ? Color.WHITE : Color.BLACK);
        childViewHolder.odd_draw1.setBackgroundColor(list.get(position).isHSchecked() ? Color.parseColor("#FF0026") : Color.WHITE);
        childViewHolder.odd_draw1.setTextColor(list.get(position).isHSchecked() ? Color.WHITE : Color.BLACK);
        //左边
        childViewHolder.odd_home.setBackgroundColor(list.get(position).isFchecked() ? Color.parseColor("#FF0026") : Color.WHITE);
        childViewHolder.odd_home.setTextColor(list.get(position).isFchecked() ? Color.WHITE : Color.BLACK);
        childViewHolder.odd_home1.setBackgroundColor(list.get(position).isHFchecked() ? Color.parseColor("#FF0026") : Color.WHITE);
        childViewHolder.odd_home1.setTextColor(list.get(position).isHFchecked() ? Color.WHITE : Color.BLACK);
        //右边
        childViewHolder.odd_draw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isSchecked()) {
                    clearValue(1,list.get(position));
                } else {
                    String value = "0";
                    if(pager==0||pager==1||pager==3) {
                        value=("3");
                    }else if(pager==2){
                        value=("0");
                    }
                    setValue(1,list.get(position),value,rqsflists.get(1)[2]);
                }
                list.get(position).setSchecked(!list.get(position).isSchecked());
                rqsfAdd(list.get(position));
            }
        });
        //左边
        childViewHolder.odd_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isFchecked()) {
                    clearValue(0,list.get(position));
                } else {
                    String value = "0";
                    if(pager==0||pager==1||pager==3) {
                        value=("0");
                    }else if(pager==2){
                        value=("3");
                    }
                    setValue(0,list.get(position),value,rqsflists.get(1)[1]);
                }
                list.get(position).setFchecked(!list.get(position).isFchecked());
                rqsfAdd(list.get(position));
            }
        });

        //右边
        childViewHolder.odd_draw1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isHSchecked()) {
                    clearValue(3,list.get(position));
                } else {
                    String value = "0";
                    if(pager==0||pager==1) {
                        value=("3");
                    }else if(pager==2||pager==3){
                        value=("0");
                    }
                    //记录当前数组长度 用来取消点击的时候移除
                    setValue(3,list.get(position),value,rqsflists.get(2)[1]);
                }
                list.get(position).setHSchecked(!list.get(position).isHSchecked());
                rqsfAdd(list.get(position));
            }
        });

        //左边
        childViewHolder.odd_home1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isHFchecked()) {
                    clearValue(2,list.get(position));
                } else {
                    String value = "0";
                    if(pager==0||pager==1) {
                        value=("0");
                    }else if(pager==2||pager==3){
                        value=("3");
                    }
                    //记录当前数组长度 用来取消点击的时候移除
                    setValue(2,list.get(position),value,rqsflists.get(2)[2]);
                }
                list.get(position).setHFchecked(!list.get(position).isHFchecked());
                rqsfAdd(list.get(position));
            }
        });
        childViewHolder.betting_item_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onItemChanged(position, v, parent);
            }
        });

        return convertView;
    }

    void updateData(JclqItme rqsfItme){
        rqsfItme.getOdd_sp().clear();
        rqsfItme.getLists().clear();
        Integer[] odd_key = rqsfItme.getOddSpMap().keySet().toArray(new Integer[]{});
        Arrays.sort(odd_key);
        for  (int key : odd_key) {
            rqsfItme.getOdd_sp().add(rqsfItme.getOddSpMap().get(key));
        }

        Integer[] list_key = rqsfItme.getListMap().keySet().toArray(new Integer[]{});
        Arrays.sort(list_key);
        for  (int key : list_key) {
            rqsfItme.getLists().add(rqsfItme.getListMap().get(key));
        }
    }

    //key是按照ui排列顺序从做往右数
    void clearValue(int key,JclqItme rqsfItme){
        rqsfItme.getListMap().remove(key);
        rqsfItme.getOddSpMap().remove(key);
    }

    //key是按照ui排列顺序从做往右数
    void setValue(int key,JclqItme rqsfItme,String value1,String value2){
        rqsfItme.getListMap().put(key,value1);
        rqsfItme.getOddSpMap().put(key,value2);
    }

    //如果数据有更新 实现这个方法
    private IAdapterChange listener;
    public void setAdapterChageListener(IAdapterChange listener){
        this.listener = listener;
    }

    static class ChildViewHolder {

        TextView home_team_name;
        TextView away_team_name;
        TextView odd_home,odd_home1;
        TextView odd_draw,odd_draw1;
        ImageView betting_item_delete;
        TextView jclq_fen_name;
        LinearLayout ll,ll1,ll2;

        public ChildViewHolder(View view) {
            betting_item_delete = (ImageView) view.findViewById(R.id.betting_item_delete);
            home_team_name = (TextView) view.findViewById(R.id.home_team_name);
            away_team_name = (TextView) view.findViewById(R.id.away_team_name);
            odd_home = (TextView) view.findViewById(R.id.odd_home);
            odd_draw = (TextView) view.findViewById(R.id.odd_draw);
            odd_home1 = (TextView) view.findViewById(R.id.odd_home1);
            odd_draw1 = (TextView) view.findViewById(R.id.odd_draw1);
            jclq_fen_name = (TextView) view.findViewById(R.id.jclq_fen_name);
            ll = (LinearLayout) view.findViewById(R.id.ll);
            ll1 = (LinearLayout) view.findViewById(R.id.ll1);
            ll2 = (LinearLayout) view.findViewById(R.id.ll2);
        }
    }

    private void rqsfAdd(JclqItme item) {
        updateData(item);
        if (item.isSchecked() || item.isFchecked() || item.isHFchecked() || item.isHSchecked()) {
            if (!Utils.JCLQ_ITEM_LIST.contains(item)) {
                Utils.JCLQ_ITEM_LIST.add(item);
            }
        } else {
            if (Utils.JCLQ_ITEM_LIST.contains(item)) {
                Utils.JCLQ_ITEM_LIST.remove(item);
            }
        }
        notifyDataSetChanged();
        changed.onClickSetChangedText();
    }
}
